package net.hn.hnms.biz.energy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.energy.domain.WaterSettlement;
import net.hn.hnms.biz.energy.mapper.WaterSettlementMapper;
import net.hn.hnms.biz.energy.request.WaterSettlementReq;
import net.hn.hnms.biz.energy.service.WaterSettlementService;
import net.hn.hnms.biz.energy.vo.WaterSettlementVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WaterSettlementServiceImpl implements WaterSettlementService {

    @Autowired
    private WaterSettlementMapper waterSettlementMapper;

    @Override
    public int insert(WaterSettlementReq waterSettlementReq) {
        WaterSettlement insertObj = new WaterSettlement();
        BeanUtil.copyProperties(waterSettlementReq, insertObj);
        return waterSettlementMapper.insert(insertObj);
    }

    @Override
    public void updateDeleteByIds(List<String> idList) {
        waterSettlementMapper.updateDeleteByIds(idList);
    }

    @Override
    public void updateById(WaterSettlementReq waterSettlementReq) {
        WaterSettlement updateObj = new WaterSettlement();
        BeanUtil.copyProperties(waterSettlementReq, updateObj);
        int updateCount = waterSettlementMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }

    @Override
    public WaterSettlement selectDetailById(String id) {
        WaterSettlement waterSettlement = waterSettlementMapper.selectById(id);
        if (waterSettlement == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return waterSettlement;
    }

    @Override
    public ResponsePage<WaterSettlementVo> selectPageList(WaterSettlementReq waterSettlementReq, PageQuery pageQuery) {
//        Page<WaterSettlement> page = waterSettlementMapper.selectPage(pageQuery.build(), buildQuery(waterSettlementReq).orderByDesc(WaterSettlement::getCreateTime));
        Page<WaterSettlementVo> page = waterSettlementMapper.selectPageList(pageQuery.build(), waterSettlementReq);
//        List<WaterSupplyCompanyVo> list =page.getRecords().stream().map(o ->convert(o, WaterSupplyCompanyVo.class)).collect(Collectors.toList());
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }
    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<ExecutionOther> 查询条件
     */
    private LambdaQueryWrapper<WaterSettlement> buildQuery(WaterSettlementReq req){
        return new LambdaQueryWrapper<WaterSettlement>()
                .eq(StrUtil.isNotBlank(req.getWaterPlaceId()), WaterSettlement::getWaterPlaceId, req.getWaterPlaceId())
                .ge(req.getBeginDateTime() != null, WaterSettlement::getCreateTime, req.getBeginDateTime())
                .le(req.getEndDateTime() != null, WaterSettlement::getCreateTime, req.getEndDateTime());
    }
}

